package com.yzjs.localhost.service.analysis;

import com.yzjs.localhost.dictionaries.NeusoftDataStatus;
import com.yzjs.localhost.dto.UserRightsDTO;
import com.yzjs.localhost.entity.tNeusoftSysDept.TNeusoftSysDeptEntity;
import com.yzjs.localhost.entity.tNeusoftSysMenu.TNeusoftSysMenuEntity;
import com.yzjs.localhost.entity.tNeusoftSysRole.TNeusoftSysRoleEntity;
import com.yzjs.localhost.entity.tNeusoftSysRoleDept.TNeusoftSysRoleDeptEntity;
import com.yzjs.localhost.entity.tNeusoftSysRoleMenu.TNeusoftSysRoleMenuEntity;
import com.yzjs.localhost.entity.tNeusoftSysUser.TNeusoftSysUserEntity;
import com.yzjs.localhost.entity.tNeusoftSysUserRole.TNeusoftSysUserRoleEntity;
import com.yzjs.localhost.service.tNeusoftSysDept.TNeusoftSysDeptService;
import com.yzjs.localhost.service.tNeusoftSysMenu.TNeusoftSysMenuService;
import com.yzjs.localhost.service.tNeusoftSysRole.TNeusoftSysRoleService;
import com.yzjs.localhost.service.tNeusoftSysRoleDept.TNeusoftSysRoleDeptService;
import com.yzjs.localhost.service.tNeusoftSysRoleMenu.TNeusoftSysRoleMenuService;
import com.yzjs.localhost.service.tNeusoftSysUser.TNeusoftSysUserService;
import com.yzjs.localhost.service.tNeusoftSysUserRole.TNeusoftSysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户解析
 */
@Service
public class UserAnalysisService {


    @Autowired
    private TNeusoftSysUserService neusoftSysUserService ;

    @Autowired
    private TNeusoftSysUserRoleService neusoftSysUserRoleService;

    @Autowired
    private TNeusoftSysRoleService neusoftSysRoleService;

    @Autowired
    private TNeusoftSysDeptService  neusoftSysDeptService;
    @Autowired
    private TNeusoftSysRoleDeptService neusoftSysRoleDeptService ;

    @Autowired
    private TNeusoftSysMenuService  neusoftSysMenuService;

    @Autowired
    private TNeusoftSysRoleMenuService neusoftSysRoleMenuService;




    // 加载所有用户，以及拥有的权限集合
    public Map<Long, UserRightsDTO> LoadUserPower(){
        Map<Long, UserRightsDTO>  userMap = new HashMap<Long, UserRightsDTO>();
        /**
         * 1. 获取所有用户信息列表
         * 2. 获取所有用户角色中间记录
         * 3. 获取所有角色记录
         * 4. 获取所有部门记录
         * 5. 获取部门角色中间表
         * 6. 获取角色菜单中间表
         * 7. 获取菜单信息内容
         * 8. 过滤筛选比对角色权限，部门权限，菜单权限
         */
        TNeusoftSysUserEntity userEntity = new TNeusoftSysUserEntity();
        userEntity.setStatus(NeusoftDataStatus.DATA_NORMAL);
        List<TNeusoftSysUserEntity> users = neusoftSysUserService.findList(userEntity);

        TNeusoftSysUserRoleEntity  userRoleEntity = new TNeusoftSysUserRoleEntity();
        List<TNeusoftSysUserRoleEntity> userRoles = neusoftSysUserRoleService.findList(userRoleEntity);

        TNeusoftSysRoleEntity roleEntity = new TNeusoftSysRoleEntity();
        roleEntity.setStatus(NeusoftDataStatus.DATA_NORMAL);
        List<TNeusoftSysRoleEntity>  roles = neusoftSysRoleService.findList(roleEntity);

        TNeusoftSysDeptEntity deptEntity =new TNeusoftSysDeptEntity();
        deptEntity.setStatus(NeusoftDataStatus.DATA_NORMAL);
        List<TNeusoftSysDeptEntity> depts = neusoftSysDeptService.findList(deptEntity);

        TNeusoftSysRoleDeptEntity roleDeptEntity = new TNeusoftSysRoleDeptEntity();
        List<TNeusoftSysRoleDeptEntity>  roleDepts = neusoftSysRoleDeptService.findList(roleDeptEntity);

        TNeusoftSysRoleMenuEntity roleMenuEntity = new TNeusoftSysRoleMenuEntity();
        List<TNeusoftSysRoleMenuEntity>  roleMenus =  neusoftSysRoleMenuService.findList(roleMenuEntity);

        TNeusoftSysMenuEntity menuEntity = new TNeusoftSysMenuEntity();
        menuEntity.setStatus(NeusoftDataStatus.DATA_NORMAL);
        menuEntity.setMenuType("F");
        List<TNeusoftSysMenuEntity> menus = neusoftSysMenuService.findList(menuEntity);


        // 比对碰撞出来所关联角色权限，以及部门权限
        for (TNeusoftSysUserEntity user:users){
            UserRightsDTO  rightsDTO =  new UserRightsDTO();
            // 获取用户角色权限
            Map<String, TNeusoftSysMenuEntity> roleMenusMap = getUserRoleAuth(user,userRoles,roles,roleMenus,menus);
            rightsDTO.setRoleMenus(roleMenusMap);


            // 获取角色部门权限
            Map<String, TNeusoftSysMenuEntity> deptMenus = getUserDeptAuth(user,depts,roleDepts,roles,roleMenus,menus);
            rightsDTO.setDeptMenus(deptMenus);


            Map<String, TNeusoftSysRoleEntity>  rolesMap = getAllRoles(user,roleDepts,userRoles,roles);
            //获取角色信息
            rightsDTO.setRoles(rolesMap);

            rightsDTO.setUserId(user.getUserId());
            rightsDTO.setNickName(user.getNickName());
            if (rolesMap.entrySet() != null ){
                Iterator it = rolesMap.entrySet().iterator();
                if(it.hasNext()){
                    rightsDTO.setRoleId(rolesMap.entrySet().iterator().next().getValue().getRoleId());
                    rightsDTO.setRoleName((rolesMap.entrySet().iterator().next().getValue().getRoleName()));
                }
            }
            TNeusoftSysDeptEntity deptEntity1 = depts.stream().filter(dept -> dept.getDeptId().equals(user.getDeptId()) ).findFirst().get();
            rightsDTO.setDeptId(deptEntity1.getDeptId());
            rightsDTO.setDeptName(deptEntity1.getDeptName());
            userMap.put(user.getUserId(),rightsDTO);
        }

        return userMap;
    }

    /**
     * 根据用户信息获取所属角色权限，以及部门权限
     * @param user
     */
    public Map<String, TNeusoftSysMenuEntity> getUserRoleAuth(TNeusoftSysUserEntity user,List<TNeusoftSysUserRoleEntity> userRoles,
                                                              List<TNeusoftSysRoleEntity>  roles,List<TNeusoftSysRoleMenuEntity>  roleMenus,List<TNeusoftSysMenuEntity> menus){
        // 过滤用户角色管理id
        List<TNeusoftSysUserRoleEntity> userRoleInfos = userRoles.stream().filter(userRole -> userRole.getUserId().equals(user.getUserId()) ).collect(Collectors.toList());
        List<TNeusoftSysRoleEntity>  rolesInfoAll = new LinkedList<>();
        for (TNeusoftSysUserRoleEntity userRoleInfo:userRoleInfos ){
            List<TNeusoftSysRoleEntity>  rolesInfo = roles.stream().filter(role -> role.getRoleId().equals(userRoleInfo.getRoleId()) ).collect(Collectors.toList());
            rolesInfoAll.addAll(rolesInfo);
        }

        List<TNeusoftSysRoleMenuEntity>  roleMenusAll = new LinkedList<TNeusoftSysRoleMenuEntity>();
        for (TNeusoftSysRoleEntity sysRole : rolesInfoAll){
            List<TNeusoftSysRoleMenuEntity>  roleMenuList  = roleMenus.stream().filter(roleMenu -> roleMenu.getRoleId().equals(sysRole.getRoleId()) ).collect(Collectors.toList());
            roleMenusAll.addAll(roleMenuList);
        }
        List<TNeusoftSysMenuEntity> userRolemesnus = new LinkedList<>();
        for (TNeusoftSysRoleMenuEntity roleMenu :roleMenusAll ){
            menus.forEach(d ->{
                if(d.getMenuId().equals(roleMenu.getMenuId())){
                    userRolemesnus.add(d);
                }
            });
        }

        Map<String, TNeusoftSysMenuEntity> roleMenusMap  = new HashMap<>();
        for (TNeusoftSysMenuEntity menu :userRolemesnus){
            roleMenusMap.put(menu.getPerms(),menu);
        }

        return  roleMenusMap;
    }

    /**
     * 获取部门权限信息
     * @return
     */
    public Map<String, TNeusoftSysMenuEntity> getUserDeptAuth(TNeusoftSysUserEntity user,List<TNeusoftSysDeptEntity> depts , List<TNeusoftSysRoleDeptEntity>  roleDepts,
                                                              List<TNeusoftSysRoleEntity>  roles,List<TNeusoftSysRoleMenuEntity>  roleMenus,List<TNeusoftSysMenuEntity> menus){

        TNeusoftSysDeptEntity  deptEntity1 = depts.stream().filter(dept -> dept.getDeptId().equals(user.getDeptId())).findFirst().get();

        // 获取部门权限信息
        List<TNeusoftSysRoleDeptEntity>  roleDeptList =  roleDepts.stream().filter(roleDept -> roleDept.getDeptId().equals(deptEntity1.getDeptId())).collect(Collectors.toList());
        List<TNeusoftSysRoleEntity>  rolesInfosAll = new LinkedList<>();
        for (TNeusoftSysRoleDeptEntity roleDept : roleDeptList){
            List<TNeusoftSysRoleEntity>  rolesInfos =  roles.stream().filter(role ->role.getRoleId().equals(roleDept.getRoleId())).collect(Collectors.toList());
            rolesInfosAll.addAll(rolesInfos);
        }
        List<TNeusoftSysRoleMenuEntity>  roleMenusAll = new LinkedList<TNeusoftSysRoleMenuEntity>();
        for (TNeusoftSysRoleEntity sysRole :rolesInfosAll ){
            List<TNeusoftSysRoleMenuEntity>  roleMenuList  = roleMenus.stream().filter(roleMenu -> roleMenu.getRoleId().equals(sysRole.getRoleId()) ).collect(Collectors.toList());
            roleMenusAll.addAll(roleMenuList);
        }

        List<TNeusoftSysMenuEntity> userRolemesnus = new LinkedList<>();
        for (TNeusoftSysRoleMenuEntity roleMenu :roleMenusAll ){
            menus.forEach(d ->{
                if(d.getMenuId().equals(roleMenu.getMenuId())){
                    userRolemesnus.add(d);
                }
            });
        }
        Map<String, TNeusoftSysMenuEntity> deptMenusMap  = new HashMap<>();
        for (TNeusoftSysMenuEntity menu :userRolemesnus){
            deptMenusMap.put(menu.getPerms(),menu);
        }
        return deptMenusMap;
    }

    public Map<String, TNeusoftSysRoleEntity>  getAllRoles(TNeusoftSysUserEntity user,List<TNeusoftSysRoleDeptEntity>  roleDepts,List<TNeusoftSysUserRoleEntity> userRoles,
                             List<TNeusoftSysRoleEntity>  roles){
        List<TNeusoftSysRoleDeptEntity>  roleDeptList =  roleDepts.stream().filter(roleDept -> roleDept.getDeptId().equals(user.getDeptId())).collect(Collectors.toList());
        List<TNeusoftSysRoleEntity>  rolesInfosAll = new LinkedList<>();
        for (TNeusoftSysRoleDeptEntity roleDept : roleDeptList){
            List<TNeusoftSysRoleEntity>  rolesInfos =  roles.stream().filter(role ->role.getRoleId().equals(roleDept.getRoleId())).collect(Collectors.toList());
            rolesInfosAll.addAll(rolesInfos);
        }

        List<TNeusoftSysUserRoleEntity> userRoleInfos = userRoles.stream().filter(userRole -> userRole.getUserId().equals(user.getUserId()) ).collect(Collectors.toList());
        for (TNeusoftSysUserRoleEntity userRoleInfo:userRoleInfos ){
            List<TNeusoftSysRoleEntity>  rolesInfo = roles.stream().filter(role -> role.getRoleId().equals(userRoleInfo.getRoleId()) ).collect(Collectors.toList());
            rolesInfosAll.addAll(rolesInfo);
        }

        Map<String, TNeusoftSysRoleEntity>  rolesMap = new HashMap<>();
        for (TNeusoftSysRoleEntity  role:rolesInfosAll){
            rolesMap.put(role.getRoleKey(),role);
        }

        return rolesMap;
    }

}
